/*
 *    Copyright © OpenAtom Foundation.
 *
 *    Licensed under the Apache License, Version 2.0 (the "License");
 *    you may not use this file except in compliance with the License.
 *    You may obtain a copy of the License at
 *
 *         http://www.apache.org/licenses/LICENSE-2.0
 *
 *    Unless required by applicable law or agreed to in writing, software
 *    distributed under the License is distributed on an "AS IS" BASIS,
 *    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *    See the License for the specific language governing permissions and
 *    limitations under the License.
 */

package com.inspur.edp.bff.spi.gencommon;

import com.inspur.edp.cef.entity.config.CefConfig;
import com.inspur.edp.cef.gencommon.config.CefConfigManager;
import com.inspur.edp.formserver.viewmodel.extendinfo.api.GspVoExtendInfoService;
import com.inspur.edp.formserver.viewmodel.extendinfo.entity.GspVoExtendInfo;
import com.inspur.edp.lcm.metadata.api.entity.ProcessMode;
import io.iec.edp.caf.commons.utils.SpringBeanUtils;


public class FsConfigManager extends CefConfigManager {

    private static FsConfigManager instance;

    public static FsConfigManager getInstance() {
        if (instance == null)
            instance = new FsConfigManager();
        return instance;
    }

    //	@Override
    protected String getConfigFilePath() {
        return "fs_runtime.json";
    }

    //	@Override
    protected String getConfigType() {
        return "BFF";
    }

    @Override
    protected CefConfig getCefConfigByDb(String configId) {
        GspVoExtendInfoService gspVoExtendInfoService = SpringBeanUtils.getBean(GspVoExtendInfoService.class);
        // 通过id获取
        GspVoExtendInfo voExisting = gspVoExtendInfoService.getVoExtendInfoByConfigId(configId);
        if (voExisting == null) {
            voExisting = gspVoExtendInfoService.getVoExtendInfo(configId);
        }
        if (voExisting == null) {
            int shortIndex = configId.lastIndexOf(".");
            if (shortIndex > 0) {
                String temConfigId = configId.substring(shortIndex + 1);
                voExisting = gspVoExtendInfoService.getVoExtendInfoByConfigId(temConfigId);
            }
        }
        if (voExisting == null) {
            return null;
        }
        if (voExisting.getVoConfigCollectionInfo() != null && voExisting.getVoConfigCollectionInfo().getConfig().getID() != null && !voExisting.getVoConfigCollectionInfo().getConfig().getID().isEmpty() && ProcessMode.generation == voExisting.getVoConfigCollectionInfo().getProjectType()) {
            return voExisting.getVoConfigCollectionInfo().getConfig();
        } else {
            return null;
        }
    }
}
